package com.learning.jquizenglish.utility;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

public class SystemCheckUtil {

	
	/**
  	 * check if system can handle for match a intent or not
  	 * @author MinhTDH
  	 * @param ctx
  	 * @param intent
  	 * @return boolean
  	 *********************************************************
  	 */
  	public static boolean isAvailable(Context ctx, Intent intent) {
		   final PackageManager mgr = ctx.getPackageManager();
		   List<ResolveInfo> list =
		      mgr.queryIntentActivities(intent, 
		         PackageManager.MATCH_DEFAULT_ONLY);
		   return list.size() > 0;
		}
  	
  	public static boolean hasHoneycomb() {
  		return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? true : false);
  	}
  	
  	public static boolean isOnline(Context context) {
		 
		 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
				 										Context.CONNECTIVITY_SERVICE);		 
		 if(cm.getActiveNetworkInfo() == null)
			 return false;
		 else
			 return cm.getActiveNetworkInfo().isConnected();

	}
  	/**
	 * check if network available require permission ACCESS_NETWORK_STATE,
	 * ACCESS_WIFI_STATE
	 * 
	 * @author MinhTDH
	 * @param context
	 * @return boolean
	 ********************************************************* 
	 */

	public static boolean checkStatusNetwork(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo wifiNetwork = cm
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiNetwork != null && wifiNetwork.isConnected()) {
			return true;
		}

		NetworkInfo mobileNetwork = cm
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mobileNetwork != null && mobileNetwork.isConnected()) {
			return true;
		}

		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected()) {
			return true;
		}

		return false;
	}
	
	/**
  	 * check if system have feature such as camera, bluetooth, ...
  	 * @author MinhTDH
  	 * @param context
  	 * @param feature : one of the features in {@link PackageManager}
  	 * @return boolean
  	 *********************************************************
  	 */
  	public static boolean checkFeature(Context context, String feature) {
  		boolean ret = false;
  		if (context != null && context.getPackageManager() != null) {
  			ret = context.getPackageManager().hasSystemFeature(feature);
  		}
  		return ret;
  	}
  	
  	public static boolean checkCamera(Context context) {
  		return checkFeature(context, PackageManager.FEATURE_CAMERA);
  	}
}
